package cn.springboot.erp.controller;

import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.PurchaseOrderDetails;
import cn.springboot.erp.service.PurchaseOrderDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "采购单据")
@RequestMapping("purchaseOrderDetails")
public class PurchaseOrderDetailsController {

    @Resource
    private PurchaseOrderDetailsService purchaseOrderDetailsService;

    @ApiOperation("数据分页")
    @PostMapping("page")
    public ResponseResult listPage(@RequestBody QueryParams<PurchaseOrderDetails> queryParams){
        return purchaseOrderDetailsService.listPage(queryParams);
    }

    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        PurchaseOrderDetails one = purchaseOrderDetailsService.getById(id);
        return ResponseResult.Success().data("one",one);
    }

    @ApiOperation("根据id删除")
    @GetMapping("deleteById/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        purchaseOrderDetailsService.removeById(id);
        return ResponseResult.Success().msg("删除成功");
    }

    @ApiOperation("添加或编辑")
    @PostMapping("insertOrUpdate")
    public ResponseResult insertOrUpdate(@RequestBody PurchaseOrderDetails purchaseOrderDetails){
        if (purchaseOrderDetails.getId()!=null){
            purchaseOrderDetailsService.saveOrUpdate(purchaseOrderDetails);
            return ResponseResult.Success().msg("修改成功");
        }
        purchaseOrderDetailsService.saveOrUpdate(purchaseOrderDetails);
        return ResponseResult.Success().msg("添加成功");
    }
}
